package com.inet.request;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONArray;

import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import com.inet.consoleservices.MainActivity;
import com.inet.consoleservices.MyApplication;
import com.inet.util.HttpUtil;
import com.inet.util.L;
import com.inet.util.SystemPropertiesProxy;
import com.inet.util.Utils;

public class ServerRequest {

	/**
	 * 向服务器提交注册的ClientId
	 * 
	 * @param imei
	 * @param clientId
	 */
	public static void registerClient(Context context, String imei,
			String clientId) {
		L.i("imei==" + imei + "--clientId==" + clientId);
		if (Utils.isEmpty(imei) || Utils.isEmpty(clientId)) {
			return;
		}
		Map<String, String> maps = new HashMap<String, String>();
		maps.put("imei", imei);
		maps.put("registrationId", clientId);
		maps.put("getui", "true");
		maps.put("version", SystemPropertiesProxy.get(context,
				"ro.build.display.id", "noVersion"));
		maps.put("project", MyApplication.customId);
		String response = HttpUtil.sendPost(maps,
				Utils.getPropertiesValue("post_send_client_id"));
		L.i("---response==" + response);
		long ida2a2 = -1;
		if (!Utils.isEmpty(response)) {
			JSONObject object = null;
			try {
				object = new JSONObject(response);

				// String msg= object.getString("msg");
				// JSONObject msgJson=new JSONObject(msg);
				ida2a2 = object.getLong("ida2a2");
				Settings.System.putLong(context.getContentResolver(), "ida2a2",
						ida2a2);
				// 注册成功，发送qrcodeUrl给MainActivty生成二维码
				String accesstoken = ServerRequest
						.getCustomToken(MyApplication.customId);
				L.i("---accesstoken==" + accesstoken + "---ida2a2==" + ida2a2);
				String qrcodeUrl = getCustomQrcode(accesstoken, ida2a2);
				MainActivity.qrcodeUrl = qrcodeUrl;

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		try {
			Settings.System.putLong(context.getContentResolver(), "ida2a2",
					ida2a2);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param context
	 * @param imei
	 */
	public static void sendICCID(final Context context, String imei){
		try {

			TelephonyManager mTelephonyManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			final String iccidCode = mTelephonyManager.getSimSerialNumber();
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {

						Map<String, String> maps = new HashMap<String, String>();
						maps.put("iccid", iccidCode);
						maps.put("imei", Utils.getImei(context));
						HttpUtil.sendPost(maps,
								Utils.getPropertiesValue("post_sendICCID"));
						L.i("sendICCID to server==" + iccidCode);
					} catch (Exception e) {
						L.i("sendICCIC error"+e.toString());
					}
				}
			}).start();
		} catch (Exception e) {
		}
	}
	
	public static void uploadCloseState(Context context, String imei) {

	}

	/**
	 * 被动上传位置获取点
	 * 
	 * @param lon
	 * @param lat
	 * @param speed
	 * @param time
	 */
	public static void uploadCarPoint(String point, String imei, String speed) {
		try {
			L.i("serverRequest  -- uploadCarPoint");
			Map<String, String> maps = new HashMap<String, String>();
			maps.put("time", Utils.format.format(new Date()));
			maps.put("point", point);
			maps.put("speed", speed);
			maps.put("imei", imei);
			HttpUtil.sendPost(maps, Utils.getPropertiesValue("post_pushPoint"));
		} catch (Exception e) {
		}
	}

	/**
	 * 主动上传轨迹点
	 * 
	 * @param imei
	 * @param list
	 */
	public static void uploadLocusPoint(String imei, List<JSONObject> list) {
		try {

			L.i("serverRequest   -- uploadLocusPoint");
			L.i("serverRequest   -- uploadLocusPoint list=="
					+ JSONArray.toJSONString(list));
			Map<String, String> maps = new HashMap<String, String>();
			maps.put("imei", imei);
			maps.put("list", JSONArray.toJSONString(list));
			HttpUtil.sendPost(maps, Utils.getPropertiesValue("post_batchLocus"));
		} catch (Exception e) {
		}
	}

	/**
	 * 關閉系統時需要請求服務器將狀態上傳
	 * 
	 * @param imei
	 */
	public static void closeSystem(String imei) {
		try {
			Map<String, String> maps = new HashMap<String, String>();
			maps.put("imei", imei);
			maps.put("time", Utils.format.format(new Date()));
			HttpUtil.sendPost(maps,
					Utils.getPropertiesValue("post_close_system"));
		} catch (Exception e) {
		}
	}

	/**
	 * 車輛駛出電子圍欄範圍時給予服務器提示
	 * 
	 * @param imei
	 */
	public static void crawlWarning(String imei) {
		try {
			L.i("serverRequest  -- crawlWarning");
			Map<String, String> maps = new HashMap<String, String>();
			maps.put("imei", imei);
			maps.put("time", Utils.format.format(new Date()));
			HttpUtil.sendPost(maps,
					Utils.getPropertiesValue("post_crawl_warning"));
		} catch (Exception e) {
		}
	}

	/**
	 * 获取对应编号的客户信息
	 * 
	 * @return
	 */
	public static String getCustomInfo(String customId) {
		String result = null;
		try {
			L.i("serverRequest  -- getCustomInfo");
			L.i("serverRequest  -- customId"+customId);
			Map<String, String> maps = new HashMap<String, String>();
			maps.put("ida2a2", customId);
			result = HttpUtil.sendPost(maps,
					Utils.getPropertiesValue("post_getCustomInfo"));
		} catch (Exception e) {
		}
		return result;
	}

	/**
	 * 获取对应编号的客户的token值
	 * 
	 * @return
	 */
	public static String getCustomToken(String customId) {
		L.i("serverRequest  -- getCustomToken");
		String token = null;
		try {
			String result = getCustomInfo(customId);
			if (Utils.isEmpty(result)) {
				return token;
			}
			JSONObject json;
			try {
				json = new JSONObject(result);
				token = json.getString("token");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
		}
		L.i("token==" + token);
		return token;
	}

	/**
	 * 反馈状态给服务器
	 * 
	 * @param imei
	 * @param state
	 *            开启的状态
	 * @param url
	 *            开启的类型
	 */
	public static void startSuccess(String imei, String state, String url) {
		try {
			Map<String, String> maps = new HashMap<String, String>();
			maps.put("imei", imei);
			maps.put("state", state);
			HttpUtil.sendPost(maps, url);
		} catch (Exception e) {
		}
	}

	/**
	 * 开始请求，获取二维码所需的url
	 */
	public static String getCustomQrcode(String token, long ida2a2) {
		String url = null;
		try {
//			ida2a2=1012572;
//			token="QQaGSwx7UA7BMn2v5sKcmIR1Agfgb_aipPzJz7qg7JgnWHrN0f_L5BxInBIEgZdF6UQ3B7bUxCQwHN6iQbKyNr5_59Qy5_VYtKRyX6bJNE42yZWEChJVII2vm4XO5RpgPEDjABAZON";
			String str = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\":"
					+ ida2a2 + "}}}";
			String result2 = HttpUtil.sendQrcodePost(str,
					"https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="
							+ token);
			L.i("result2==" + result2);
			JSONObject json2 = new JSONObject(result2);
			url = json2.getString("url");
			L.i("url==" + url);

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return url;

	}

}
